﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace DigDes.DS2012.WebUI
{
    public partial class DislocationHistory : System.Web.UI.Page
    {
        SelectorList selectorList = new SelectorList();

        protected void Page_Load(object sender, EventArgs e)
        {
            updateSelectors();
            updateGrid();
        }

        private void updateSelectors()
        {
            selectorList.Clear();

            insertSelector("CarNumber", "usp_Dislocation_CarNumber_get", "PlaceHolder1");
            insertSelector("Operation", "usp_Operation_get", "PlaceHolder2");
            insertSelector("StationOfOperation", "usp_Stations_get", "PlaceHolder3");
            insertSelector("ArrivalStation", "usp_Stations_get", "PlaceHolder4");
            insertSelector("DepartureStation", "usp_Stations_get", "PlaceHolder5");
        }

        private void insertSelector(string name, string storedProcedureName, string parentId)
        {
            var objDataSource = new ObjectDataSource("DigDes.DS2012.BusinessLogic.DislocationHistoryManager", "GetReference");
            objDataSource.SelectParameters.Add(new Parameter("storedProcedureName", TypeCode.String, storedProcedureName));
            objDataSource.ID = String.Format("ds{0}", name);
            mainForm.Controls.Add(objDataSource);
   
            selectorList.Add(new Selector(name, storedProcedureName));

            mainForm.FindControl(parentId).Controls.Add(selectorList[name]);
        }
    
        private void updateGrid()
        {
            dsDislocationHistory.SelectParameters.Clear();
            foreach (var tempParameter in getParameters())
                dsDislocationHistory.SelectParameters.Add(tempParameter);
        }

        private Parameter[] getParameters()
        {
            Parameter[] tempParameters = new Parameter[5];

            tempParameters[0] = new Parameter("fltOperation", TypeCode.Int32, Selector.GetValue(selectorList["Operation"], Selector.TypeOf.Int).Split('/')[0]);
            tempParameters[1] = new Parameter("fltCarNumber", TypeCode.String, Selector.GetValue(selectorList["CarNumber"], Selector.TypeOf.Str));
            tempParameters[2] = new Parameter("fltStationOfOperation", TypeCode.Int32, Selector.GetValue(selectorList["StationOfOperation"], Selector.TypeOf.Int).Split('/')[0]);
            tempParameters[3] = new Parameter("fltDepartureStation", TypeCode.Int32, Selector.GetValue(selectorList["DepartureStation"], Selector.TypeOf.Int).Split('/')[0]);
            tempParameters[4] = new Parameter("fltArrivalStation", TypeCode.Int32, Selector.GetValue(selectorList["ArrivalStation"], Selector.TypeOf.Int).Split('/')[0]);
            return tempParameters;
        }

        protected void Filt_Click(object sender, EventArgs e)
        {
            updateGrid();
        }
    }
}